#!/bin/sh
# PCP QA Test No. 1705
# Exercise the bpftrace PMDA - error conditions
#

seq=`basename $0`
echo "QA output created by $seq"

. ./common.bpftrace

_pmdabpftrace_check

status=1       # failure is the default!
$sudo rm -rf $tmp.* $seq.full

_prepare_pmda bpftrace
trap "_pmdabpftrace_cleanup; exit \$status" 0 1 2 3 15
_stop_auto_restart pmcd

# real QA test starts here
_pmdabpftrace_clean_autostart_scripts
cat <<EOF | _pmdabpftrace_install
# Installed by PCP QA test $seq on `date`
[dynamic_scripts]
enabled = true
auth_enabled = false

[bpftrace]
# ridiculously small for test5 running on (potentially) slow VMs
max_throughput = 4096
EOF

echo "=== bpftrace script without variabes ==="
./src/store_and_fetch bpftrace.control.register "// name: test1
tracepoint:raw_syscalls:sys_enter { }" | grep -o '"error": "no bpftrace variables or printf statements found, please include at least one variable or printf statement in your script"'
echo

echo "=== invalid probe ==="
pmstore bpftrace.control.register "// name: test2
does_not_exist { @c = count(); }"
_pmdabpftrace_wait_for_value bpftrace.scripts.test2.status '"error"'
# error message changed between bpftrace versions
pminfo -f bpftrace.scripts.test2.error \
    | grep -q "Invalid provider: 'does_not_exist'\|Unrecognized probe type: does_not_exist" \
    && echo "error message (invalid probe) found"
pminfo -f bpftrace.scripts.test2.exit_code
echo

echo "=== syntax error ==="
pmstore bpftrace.control.register "// name: test3
tracepoint:raw_syscalls:sys_enter { @c = count(; }"
_pmdabpftrace_wait_for_value bpftrace.scripts.test3.status '"error"'
pminfo -f bpftrace.scripts.test3.error
pminfo -f bpftrace.scripts.test3.exit_code
echo

echo "=== too many attached tracepoints ==="
pmstore bpftrace.control.register "// name: test4
kprobe:* { @c = count(); }"
_pmdabpftrace_wait_for_value bpftrace.scripts.test4.status '"error"'
pminfo -f bpftrace.scripts.test4.error | sed -e 's/ [1-9][0-9]* / PROBES /g'
pminfo -f bpftrace.scripts.test4.exit_code
echo

echo "=== too much output ==="
pmstore bpftrace.control.register '// name: test5
profile:hz:999 { printf("test"); }'
_pmdabpftrace_wait_for_value bpftrace.scripts.test5.status '"error"'
pminfo -f bpftrace.scripts.test5.error | sed -e 's/ [1-9][0-9]* / LIMIT /g'
echo

echo "=== duplicate script name ==="
pmstore bpftrace.control.register "// name: test6
tracepoint:raw_syscalls:sys_enter { @x = count() }"
./src/store_and_fetch bpftrace.control.register "// name: test6
tracepoint:raw_syscalls:sys_enter { @x = count() }" | grep -o '"error": "Script name .* is already in use by another script."'
echo


_pmdabpftrace_remove
status=0
exit
